//
//  CHExceptionListController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/18.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHExceptionListController.h"
#import "CHExceptionlistInfoController.h"
#import "CHExceptionScreenController.h"
#import "CHExceptionModel.h"
#import "CHExceptionCell.h"
@interface CHExceptionListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UILabel *label;
@property (nonatomic ,strong) UILabel *numLabel;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) NSInteger sum;
@property (nonatomic ,copy) NSString *areaid;
@property (nonatomic ,copy) NSString *levels;
@property (nonatomic ,copy) NSString *dateTime;

@end

@implementation CHExceptionListController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"异常企业管理";
    self.view.backgroundColor = KRGB(239, 239, 249);
    
    self.areaid = (NSString *)[Util getValue:@"areaid"];
    self.dateTime = [self GetLastDay];
    _sum = 0;
    
    [self setupRightBarButtonItem];
    
    [self buildUI];
    
    [BQActivityView showActiviTy];
    //    [super showLoading:YES];
    
    [self getData];
    
}



-(void)setupRightBarButtonItem{
    
    
    UIButton *screenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    screenBtn.bounds = CGRectMake(0, 0, 40 ,15);
    screenBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    screenBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [screenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [screenBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [screenBtn addTarget:self action:@selector(screenBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:screenBtn];
    
}


-(void)screenBtnClick{
    CHExceptionScreenController *screenVc = [[CHExceptionScreenController alloc] init];
    
    __weak typeof(self) weakSelf = self;
    screenVc.popVCBlock = ^(NSMutableDictionary *vegDic) {

        weakSelf.areaid = vegDic[@"areaid"];
        weakSelf.levels = vegDic[@"levels"];
        weakSelf.dateTime = vegDic[@"datetime"];
        [weakSelf getData];
        
    };
    
    screenVc.endTime = [self GetLastDay];
    [self.navigationController pushViewController:screenVc animated:YES];
    
}



-(void)buildUI{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 35)];
    label.text = @"   连续7天未上传数据企业总数为0家";
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self.view addSubview:label];
    self.label = label;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 35, UIScreenW /3, 40)];
    titleLabel.text = @"区域";
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0f];
    titleLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    
    UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(UIScreenW /3, 35, UIScreenW /3 *2, 40)];
    numLabel.textColor = [UIColor colorWithHexString:@"666666" alpha:1.0f];
    numLabel.text = @"连续七天未上传数据企业数量";
    numLabel.font = [UIFont systemFontOfSize:13];
    numLabel.textAlignment = NSTextAlignmentCenter;
    numLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:numLabel];
    self.numLabel = numLabel;
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 75, UIScreenW, 1)];
    line.backgroundColor = KRGB(239, 239, 249);
    [self.view addSubview:line];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 76, UIScreenW, UIScreenH - 76 - 64)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = KRGB(239, 239, 249);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}



//   返回前天的时间
- (NSString *)GetLastDay {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
    [components setDay:([components day]-1)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}




-(void)getData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.areaid;
    params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"datetime"] = self.dateTime;
    params[@"levels"] = self.levels;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovexceptionlist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.datas removeAllObjects];
        [BQActivityView hideActiviTy];

        NSLog(@"异常数据%@",result);
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHExceptionModel *model = [CHExceptionModel objectWithKeyValues:dict];
                
                [self.datas addObject:model];
            }
           
         
            NSString *typeStr = nil;
            if ([self.levels isEqualToString:@"01"]) {
                typeStr = @"一类";
            }else if([self.levels isEqualToString:@"02"]){
                
                typeStr = @"二类";
            }else{
                typeStr = @"";
            }
            self.label.text = [NSString stringWithFormat:@"   连续7天未上传数据%@企业总数为%@家",typeStr,result[@"data"][@"total"]];
            self.numLabel.text = [NSString stringWithFormat:@"连续七天未上传数据%@企业数量",typeStr];
        }
        [self.tableView reloadData];
    }];
    
    
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHExceptionCell *cell = [CHExceptionCell cellForTableView:tableView];
    NSString *userType = (NSString *)[Util getValue:@"usertype"];
    if ([userType isEqualToString:@"99"]) {
        cell.userInteractionEnabled = NO;
    }
    cell.model = self.datas[indexPath.row];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CHExceptionlistInfoController *vc = [[CHExceptionlistInfoController alloc] init];
    vc.model = self.datas[indexPath.row];
    vc.levels = self.levels;
    [self.navigationController pushViewController:vc animated:YES];
}



@end
